package com.example.base.idGenerator;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
import org.springframework.stereotype.Component;

@Component
public class CustomKeyGenerator implements IKeyGenerator {

    @Override
    public String executeSql(String incrementerName) {
        return "SELECT CONCAT('100000',SUBSTR(ifNUll(MAX(CONVERT(id, UNSIGNED INTEGER)),1000000),7)+1)AS new_id FROM user";
    }


//    // 从数据库中查询最大主键值
//    private long queryMaxIdFromDatabase() {
//        try (SqlSession sqlSession = getSqlSessionFactory().openSession();
//             Connection connection = sqlSession.getConnection();
//             Statement statement = connection.createStatement();
//             ResultSet resultSet = statement.executeQuery(QUERY_MAX_ID_SQL)) {
//            if (resultSet.next()) {
//                return resultSet.getLong(1);
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        // 如果查询失败，返回默认值0
//        return 0;
//    }
//
//    private SqlSessionFactory getSqlSessionFactory() {
//        new SqlSessionFactoryBuilder().bu
//        return null;
//    }

    @Override
    public DbType dbType() {
        return DbType.MYSQL;
    }
}

